<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.ufunc.reduceat &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../about.html" >
    <link rel="index" title="Index" href="../../genindex.html" >
    <link rel="search" title="Search" href="../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../index.html" >
    <link rel="up" title="Universal functions (ufunc)" href="../ufuncs.html" >
    <link rel="next" title="numpy.ufunc.outer" href="numpy.ufunc.outer.html" >
    <link rel="prev" title="numpy.ufunc.accumulate" href="numpy.ufunc.accumulate.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../index.html">
      <img border=0 alt="NumPy" src="../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../ufuncs.html" accesskey="U">Universal functions (<code class="xref py py-class docutils literal notranslate"><span class="pre">ufunc</span></code>)</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="numpy.ufunc.outer.html" title="numpy.ufunc.outer"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.ufunc.accumulate.html" title="numpy.ufunc.accumulate"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.ufunc.accumulate.html"
                        title="previous chapter">numpy.ufunc.accumulate</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numpy.ufunc.outer.html"
                        title="next chapter">numpy.ufunc.outer</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-ufunc-reduceat">
<h1>numpy.ufunc.reduceat<a class="headerlink" href="#numpy-ufunc-reduceat" title="Permalink to this headline">¶</a></h1>
<p>method</p>
<dl class="method">
<dt id="numpy.ufunc.reduceat">
<code class="sig-prename descclassname">ufunc.</code><code class="sig-name descname">reduceat</code><span class="sig-paren">(</span><em class="sig-param">a</em>, <em class="sig-param">indices</em>, <em class="sig-param">axis=0</em>, <em class="sig-param">dtype=None</em>, <em class="sig-param">out=None</em><span class="sig-paren">)</span><a class="headerlink" href="#numpy.ufunc.reduceat" title="Permalink to this definition">¶</a></dt>
<dd><p>Performs a (local) reduce with specified slices over a single axis.</p>
<p>For i in <code class="docutils literal notranslate"><span class="pre">range(len(indices))</span></code>, <a class="reference internal" href="#numpy.ufunc.reduceat" title="numpy.ufunc.reduceat"><code class="xref py py-obj docutils literal notranslate"><span class="pre">reduceat</span></code></a> computes
<code class="docutils literal notranslate"><span class="pre">ufunc.reduce(a[indices[i]:indices[i+1]])</span></code>, which becomes the i-th
generalized “row” parallel to <em class="xref py py-obj">axis</em> in the final result (i.e., in a
2-D array, for example, if <em class="xref py py-obj">axis = 0</em>, it becomes the i-th row, but if
<em class="xref py py-obj">axis = 1</em>, it becomes the i-th column).  There are three exceptions to this:</p>
<ul class="simple">
<li><p>when <code class="docutils literal notranslate"><span class="pre">i</span> <span class="pre">=</span> <span class="pre">len(indices)</span> <span class="pre">-</span> <span class="pre">1</span></code> (so for the last index),
<code class="docutils literal notranslate"><span class="pre">indices[i+1]</span> <span class="pre">=</span> <span class="pre">a.shape[axis]</span></code>.</p></li>
<li><p>if <code class="docutils literal notranslate"><span class="pre">indices[i]</span> <span class="pre">&gt;=</span> <span class="pre">indices[i</span> <span class="pre">+</span> <span class="pre">1]</span></code>, the i-th generalized “row” is
simply <code class="docutils literal notranslate"><span class="pre">a[indices[i]]</span></code>.</p></li>
<li><p>if <code class="docutils literal notranslate"><span class="pre">indices[i]</span> <span class="pre">&gt;=</span> <span class="pre">len(a)</span></code> or <code class="docutils literal notranslate"><span class="pre">indices[i]</span> <span class="pre">&lt;</span> <span class="pre">0</span></code>, an error is raised.</p></li>
</ul>
<p>The shape of the output depends on the size of <a class="reference internal" href="numpy.indices.html#numpy.indices" title="numpy.indices"><code class="xref py py-obj docutils literal notranslate"><span class="pre">indices</span></code></a>, and may be
larger than <em class="xref py py-obj">a</em> (this happens if <code class="docutils literal notranslate"><span class="pre">len(indices)</span> <span class="pre">&gt;</span> <span class="pre">a.shape[axis]</span></code>).</p>
<dl class="field-list">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl>
<dt><strong>a</strong><span class="classifier">array_like</span></dt><dd><p>The array to act on.</p>
</dd>
<dt><strong>indices</strong><span class="classifier">array_like</span></dt><dd><p>Paired indices, comma separated (not colon), specifying slices to
reduce.</p>
</dd>
<dt><strong>axis</strong><span class="classifier">int, optional</span></dt><dd><p>The axis along which to apply the reduceat.</p>
</dd>
<dt><strong>dtype</strong><span class="classifier">data-type code, optional</span></dt><dd><p>The type used to represent the intermediate results. Defaults
to the data type of the output array if this is provided, or
the data type of the input array if no output array is provided.</p>
</dd>
<dt><strong>out</strong><span class="classifier">ndarray, None, or tuple of ndarray and None, optional</span></dt><dd><p>A location into which the result is stored. If not provided or None,
a freshly-allocated array is returned. For consistency with
<code class="docutils literal notranslate"><span class="pre">ufunc.__call__</span></code>, if given as a keyword, this may be wrapped in a
1-element tuple.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 1.13.0: </span>Tuples are allowed for keyword argument.</p>
</div>
</dd>
</dl>
</dd>
<dt class="field-even">Returns</dt>
<dd class="field-even"><dl class="simple">
<dt><strong>r</strong><span class="classifier">ndarray</span></dt><dd><p>The reduced values. If <em class="xref py py-obj">out</em> was supplied, <em class="xref py py-obj">r</em> is a reference to
<em class="xref py py-obj">out</em>.</p>
</dd>
</dl>
</dd>
</dl>
<p class="rubric">Notes</p>
<p>A descriptive example:</p>
<p>If <em class="xref py py-obj">a</em> is 1-D, the function <em class="xref py py-obj">ufunc.accumulate(a)</em> is the same as
<code class="docutils literal notranslate"><span class="pre">ufunc.reduceat(a,</span> <span class="pre">indices)[::2]</span></code> where <a class="reference internal" href="numpy.indices.html#numpy.indices" title="numpy.indices"><code class="xref py py-obj docutils literal notranslate"><span class="pre">indices</span></code></a> is
<code class="docutils literal notranslate"><span class="pre">range(len(array)</span> <span class="pre">-</span> <span class="pre">1)</span></code> with a zero placed
in every other element:
<code class="docutils literal notranslate"><span class="pre">indices</span> <span class="pre">=</span> <span class="pre">zeros(2</span> <span class="pre">*</span> <span class="pre">len(a)</span> <span class="pre">-</span> <span class="pre">1)</span></code>, <code class="docutils literal notranslate"><span class="pre">indices[1::2]</span> <span class="pre">=</span> <span class="pre">range(1,</span> <span class="pre">len(a))</span></code>.</p>
<p>Don’t be fooled by this attribute’s name: <em class="xref py py-obj">reduceat(a)</em> is not
necessarily smaller than <em class="xref py py-obj">a</em>.</p>
<p class="rubric">Examples</p>
<p>To take the running sum of four successive values:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">add</span><span class="o">.</span><span class="n">reduceat</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">8</span><span class="p">),[</span><span class="mi">0</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span><span class="mi">7</span><span class="p">])[::</span><span class="mi">2</span><span class="p">]</span>
<span class="go">array([ 6, 10, 14, 18])</span>
</pre></div>
</div>
<p>A 2-D example:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">16</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span>
<span class="go">array([[ 0.,   1.,   2.,   3.],</span>
<span class="go">       [ 4.,   5.,   6.,   7.],</span>
<span class="go">       [ 8.,   9.,  10.,  11.],</span>
<span class="go">       [12.,  13.,  14.,  15.]])</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># reduce such that the result has the following five rows:</span>
<span class="c1"># [row1 + row2 + row3]</span>
<span class="c1"># [row4]</span>
<span class="c1"># [row2]</span>
<span class="c1"># [row3]</span>
<span class="c1"># [row1 + row2 + row3 + row4]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">add</span><span class="o">.</span><span class="n">reduceat</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
<span class="go">array([[12.,  15.,  18.,  21.],</span>
<span class="go">       [12.,  13.,  14.,  15.],</span>
<span class="go">       [ 4.,   5.,   6.,   7.],</span>
<span class="go">       [ 8.,   9.,  10.,  11.],</span>
<span class="go">       [24.,  28.,  32.,  36.]])</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># reduce such that result has the following two columns:</span>
<span class="c1"># [col1 * col2 * col3, col4]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">multiply</span><span class="o">.</span><span class="n">reduceat</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">],</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">array([[   0.,     3.],</span>
<span class="go">       [ 120.,     7.],</span>
<span class="go">       [ 720.,    11.],</span>
<span class="go">       [2184.,    15.]])</span>
</pre></div>
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>